<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="zh-CN">

<%@include file="/WEB-INF/include-head.jsp" %>
<link rel="stylesheet" href="css/pagination.css">
<link rel="stylesheet" href="ztree/zTreeStyle.css">
<script type="text/javascript" src="ztree/jquery.ztree.all-3.5.min.js"></script>
<script type="text/javascript" src="jquery/jquery.pagination.js"></script>
<script type="text/javascript" src="crowd/my-role.js"></script>
<script type="text/javascript">
    $(function () {

        // 为分页操作准备初始化数据
        window.pageNum = 1;
        window.pageSize = 5;
        window.keyword = "";

        // 调用执行分页的函数，显示分页效果
        generatePage();

        fillAuthTree();

        $("#searchBtn").click(function () {

            window.keyword = $("#searchInput").val();

            generatePage();
        });

        $("#addRoleBtn").click(function () {
            $("#addModal").modal("show");
        });

        $("#saveRoleBtn").click(function () {
            var roleName = ($("#addModal [name=roleName]").val()).trim();

            $.ajax({
                "url": "role/save.json",
                "type": "post",
                "data": {
                    "name": roleName
                },
                "dataType": "json",
                "success": function (response) {

                    var result = response.result;

                    if (result == "SUCCESS") {
                        // 提示操作成功
                        layer.msg("操作成功！！")

                        // 分页刷新，显示最后一页
                        window.pageNum = 9999999;
                        generatePage();
                    }
                    if (result == "FAILED") {
                        // 提示操作失败
                        layer.msg("操作失败！！" + response.message);
                    }

                },
                "error": function (response) {

                    // 如果发生错误，返回状态码和错误信息
                    layer.msg(response.status + response.message);
                }
            })

            // 关闭模态框
            $("#addModal").modal("hide");

            // 清空模态框
            $("#addModal [name=roleName]").val("");
        });

        //角色名称修改
        // 由于更新按钮是动态生成的，在翻页之后此方法会失效
        /*$(".pencilBtn").click(function () {
            $("#editModal").modal("show");
        });*/
        $("#rolePageBody").on("click", ".pencilBtn", function () {
            // 打开模态框
            $("#editModal").modal("show");

            // 设置回显
            var roleName = $(this).parent().prev().text();
            $("#editModal [name=roleName]").val(roleName);

            // 依据是：var pencilBtn = "<button id='"+roleId+"' ……这段代码中我们把 roleId 设置到id 属性了
            window.roleId = this.id;
        });

        // 为更新模态框中的更新按钮添加响应函数
        $("#updateRoleBtn").click(function () {

            // 获取文本框中的roleName
            var roleName = $("#editModal [name=roleName]").val();

            $.ajax({
                "url": "role/update.json",
                "type": "post",
                "data": {
                    "id": window.roleId,
                    "name": roleName
                },
                "dataType": "json",
                "success": function (response) {

                    var result = response.result;

                    if (result == "SUCCESS") {
                        // 提示操作成功
                        layer.msg("操作成功！！")

                        // 操作成功，进行分页
                        generatePage();
                    }
                    if (result == "FAILED") {
                        // 提示操作失败
                        layer.msg("操作失败！！" + response.message);
                    }

                },
                "error": function (response) {

                    // 如果发生错误，返回状态码和错误信息
                    layer.msg(response.status + response.message);
                }

            });
            $("#editModal").modal("hide");

        });


        // 模态框中确认删除的单击事件
        $("#modalDelBtn").click(function () {
            var requestBody = JSON.stringify(window.roleIdArray);
            $.ajax({
                "url": "role/remove/roleIdArray.json",
                "type": "post",
                "data": requestBody,
                "contentType": "application/json;charset=UTF-8",
                "dataType": "json",
                "success": function (response) {

                    var result = response.result;

                    if (result == "SUCCESS") {
                        // 提示操作成功
                        layer.msg("操作成功！！")

                        // 操作成功，进行分页
                        generatePage();
                    }
                    if (result == "FAILED") {
                        // 提示操作失败
                        layer.msg("操作失败！！" + response.message);
                    }

                },
                "error": function (response) {

                    // 如果发生错误，返回状态码和错误信息
                    layer.msg(response.status + response.message);
                }
            });
            $("#delRoleDiv").modal("hide");

            //将全选取消
            $("#allCheckedBox").prop("checked", false);
        });


        // 多条删除的单击事件
        $("#delRoleBtn").click(function () {

            var roleArray = [];

            $(".itemBox:checked").each(function () {
                var id = this.id;
                var name = $(this).parent().next().text();

                roleArray.push({
                    "id": id,
                    "name": name
                });
            });

            if (roleArray.length == 0) {
                layer.msg("当前未选中任何角色")
                return;
            }

            delRoleByArray(roleArray);
        });


        // 单条删除的单击事件
        $("#rolePageBody").on("click", ".removeBtn", function () {

            var roleArray = [];
            var id = this.id;
            var name = $(this).parent().prev().text();

            roleArray.push({
                "id": id,
                "name": name
            });

            delRoleByArray(roleArray);

        });

        // 全选，全不选
        $("#rolePageBody").on("click", ".itemBox", function () {
            var checkedBoxCount = $(".itemBox:checked").length;
            var totalBoxCount = $(".itemBox").length;

            $("#allCheckedBox").prop("checked", checkedBoxCount == totalBoxCount);

        });

        $("#allCheckedBox").click(function () {

            $("#rolePageBody [name=checkBox]").prop("checked", this.checked);
        });


        // 为执行分配按钮绑定单击事件
        $("#assignBtn").click(function () {

            var treeObj = $.fn.zTree.getZTreeObj("authTreeDemo");

            // 获取被选中的节点集合
            var checkedNodes = treeObj.getCheckedNodes();
            var authIdArray = [];

            // 遍历被选中的节点集合
            for (var i = 0; i < checkedNodes.length; i++) {
                // 每个元素都是一个节点
                var chedkedNode = checkedNodes[i];
                // 节点的id就是authid
                var authId = chedkedNode.id;
                // 将authid放入数组中
                authIdArray.push(authId);
            }

            // 设置请求体格式
            var requestBody = {
                //  为了服务器端 handler 方法能够统一使用 List<Integer>方式接收数据，roleId 也存入数组
                "roleId": [window.roleId],
                "authIdArray": authIdArray
            };

            requestBody = JSON.stringify(requestBody);
            $.ajax({
                "url": "assign/save/role/auth/relationship.json",
                "type": "post",
                "data": requestBody,
                "contentType": "application/json;charset=UTF-8",
                "dataType": "json",
                "success": function (response) {
                    var result = response.result;
                    if (result == "SUCCESS") {
                        layer.msg("操作成功！");
                    }
                    if (result == "FAILED") {
                        layer.msg("操作失败！" + response.message);
                    }
                }, "error": function (response) {
                    layer.msg(response.status + " " + response.statusText);
                }
            });

            // 关闭模态框
            $("#assignModal").modal("hide");
        });

    })
</script>
<body>

<%@include file="/WEB-INF/include-nav.jsp" %>
<div class="container-fluid">
    <div class="row">
        <%@include file="/WEB-INF/include-sidebar.jsp" %>
        <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <h3 class="panel-title"><i class="glyphicon glyphicon-th"></i> 数据列表</h3>
                </div>
                <div class="panel-body">
                    <form class="form-inline" role="form" style="float:left;">
                        <div class="form-group has-feedback">
                            <div class="input-group">
                                <div class="input-group-addon">查询条件</div>
                                <input id="searchInput" class="form-control has-success" type="text"
                                       placeholder="请输入查询条件">
                            </div>
                        </div>
                        <button id="searchBtn" type="button" class="btn btn-warning"><i
                                class="glyphicon glyphicon-search"></i> 查询
                        </button>
                    </form>
                    <button id="delRoleBtn" type="button" class="btn btn-danger" style="float:right;margin-left:10px;">
                        <i class=" glyphicon glyphicon-remove"></i> 删除
                    </button>
                    <button id="addRoleBtn" type="button" class="btn btn-primary" style="float:right;"><i
                            class="glyphicon glyphicon-plus"></i> 新增
                    </button>
                    <br>
                    <hr style="clear:both;">
                    <div class="table-responsive">
                        <table class="table  table-bordered">
                            <thead>
                            <tr>
                                <th width="30">ID</th>
                                <th width="30"><input id="allCheckedBox" type="checkbox"></th>
                                <th>名称</th>
                                <th width="100">操作</th>
                            </tr>
                            </thead>
                            <tbody id="rolePageBody"></tbody>
                            <tfoot>
                            <tr>
                                <td colspan="6" align="center">
                                    <div id="Pagination" class="pagination"><!-- 这里显示分页 --></div>
                                </td>
                            </tr>

                            </tfoot>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<%@include file="/WEB-INF/modal-role-add.jsp" %>
<%@include file="/WEB-INF/modal-role-edit.jsp" %>
<%@include file="/WEB-INF/modal-role-delete.jsp" %>
<%@include file="/WEB-INF/modal-role-assign-auth.jsp" %>
</body>
</html>